import 'package:auto_route/auto_route.dart';
import 'package:base/main.dart';
import 'package:flutter/material.dart';
import 'package:framework/config/constants.dart';
import 'package:framework/data/model/function_bean.dart';
import 'package:module_home/pages/home/view_models/function_vm.dart';
import 'package:module_home/route/home_router.gr.dart';
import 'package:module_home/widgets/function_grid_item.dart';

class HomeFunctionWidget extends StatelessWidget {
  FunctionVm vm;

  HomeFunctionWidget({super.key, required this.vm});

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: const EdgeInsets.all(16),
        child: Obx(
          () => GridView.builder(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4,
              childAspectRatio: 1.5,
              crossAxisSpacing: 4,
              mainAxisSpacing: 4,
            ),
            itemCount: vm.rxFunctionList.length,
            itemBuilder: (context, index) {
              final data = vm.rxFunctionList[index];
              return _buildItemView(
                data: vm.rxFunctionList[index],
                onTap: () {
                  if (data.name.toString() == Constants.MORE_MANAGER_TAG) {
                    context.router.push(const MoreManagerRoute());
                  }
                },
              );
            },
          ),
        ));
  }

  Widget _buildItemView({
    required FunctionBean data,
    GestureTapCallback? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: FunctionGridItem(
        name: data.name,
        icon: data.icon,
      ),
    );
  }
}
